package com.xuhao.examai.controller.api;

import com.xuhao.examai.model.entity.User;
import com.xuhao.examai.model.vo.ResultVO;
import com.xuhao.examai.service.ScoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 成绩管理控制器
 */
@RestController
@RequestMapping("/scores")
public class ScoreController {
    private final ScoreService scoreService;
    
    public ScoreController(ScoreService scoreService) { 
        this.scoreService = scoreService; 
    }

    /**
     * 查询特定考试和用户的成绩
     */
    @GetMapping("/exam/{examId}/user/{userId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER') or #userId == authentication.principal.id")
    public ResultVO<Integer> getScore(@PathVariable Long examId, @PathVariable Long userId) {
        return ResultVO.success(scoreService.getScore(examId, userId));
    }

    /**
     * 查询考试排名
     */
    @GetMapping("/exam/{examId}/ranking")
    public ResultVO<List<Map<String, Object>>> getRanking(@PathVariable Long examId) {
        return ResultVO.success(scoreService.getRanking(examId));
    }
    
    /**
     * 计算考试成绩
     */
    @PostMapping("/calculate/{examRecordId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER')")
    public ResultVO<Integer> calculateScore(@PathVariable Long examRecordId) {
        return ResultVO.success(scoreService.calculateScore(examRecordId));
    }
    
    /**
     * 获取当前用户的所有考试成绩
     */
    @GetMapping("/my-scores")
    public ResultVO<List<Map<String, Object>>> getMyScores(@AuthenticationPrincipal User user) {
        return ResultVO.success(scoreService.getUserScores(user.getId()));
    }
    
    /**
     * 获取指定用户的所有考试成绩（管理员或教师使用）
     */
    @GetMapping("/user/{userId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER')")
    public ResultVO<List<Map<String, Object>>> getUserScores(@PathVariable Long userId) {
        return ResultVO.success(scoreService.getUserScores(userId));
    }
    
    /**
     * 获取考试成绩分布情况
     */
    @GetMapping("/exam/{examId}/distribution")
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER')")
    public ResultVO<Map<String, Integer>> getScoreDistribution(@PathVariable Long examId) {
        return ResultVO.success(scoreService.getScoreDistribution(examId));
    }
    
    /**
     * 分析用户的知识点掌握情况
     */
    @GetMapping("/knowledge-analysis")
    public ResultVO<Map<String, Object>> analyzeMyKnowledgePoints(@AuthenticationPrincipal User user) {
        return ResultVO.success(scoreService.analyzeUserKnowledgePoints(user.getId()));
    }
    
    /**
     * 分析指定用户的知识点掌握情况（管理员或教师使用）
     */
    @GetMapping("/knowledge-analysis/{userId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER')")
    public ResultVO<Map<String, Object>> analyzeUserKnowledgePoints(@PathVariable Long userId) {
        return ResultVO.success(scoreService.analyzeUserKnowledgePoints(userId));
    }
    
    /**
     * 获取当前用户的错题本
     */
    @GetMapping("/wrong-questions")
    public ResultVO<List<Map<String, Object>>> getMyWrongQuestions(@AuthenticationPrincipal User user) {
        return ResultVO.success(scoreService.generateWrongQuestions(user.getId()));
    }
}
